package com.tech.connect.zhaofuwu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.FuWuHttp;
import com.tech.connect.model.CatChoose;

import java.util.ArrayList;
import java.util.List;

public class ZhaoFuWuFuWuCatChooseActivity extends BaseActivity {

    private RecyclerView mainRecycler;
    private RecyclerView subRecycler;
    private BaseAdapter<CatChoose, BaseHolder> mainAdapter;
    private BaseAdapter<CatChoose, BaseHolder> subAdapter;
    private List<CatChoose> mainList = new ArrayList<>();
    private List<CatChoose> subList = new ArrayList<>();
    private int selectPosition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("选择分类");
        setContentView(R.layout.activity_cat_choose);
        initView();
        loadData();
    }

    private void loadData() {
        FuWuHttp.catChooseList(new BaseEntityOb<List<CatChoose>>() {
            @Override
            public void onDataDeal(boolean isSuccess, List<CatChoose> data, String msg) {
                initMainAdapter(data);
                if (!mainList.isEmpty()) {
                    selectPosition = 0;
                    initSubAdapter(mainList.get(0).childs);
                } else {
                    selectPosition = -1;
                }
            }
        });
    }

    private void initView() {
        mainRecycler = findViewById(R.id.mainRecycler);
        subRecycler = findViewById(R.id.subRecycler);
        mainRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        mainRecycler.addItemDecoration(new HLineDivider());
        subRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    }

    private void initMainAdapter(List<CatChoose> data) {
        mainList.clear();
        if (data != null) {
            mainList.addAll(data);
        }
        if (mainAdapter == null) {
            mainAdapter = new BaseAdapter<CatChoose, BaseHolder>(R.layout.item_layout_cat_choose, mainList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    CatChoose catChoose = mainList.get(position);
                    TextView tvItem = helper.getView(R.id.tvItem);
                    View vLine = helper.getView(R.id.vLine);
                    tvItem.setTextColor(getResources().getColor(R.color.common_colorTextGray));
                    tvItem.setText(catChoose.name);
                    if (selectPosition == position) {
                        tvItem.setBackgroundResource(R.color.common_colorGray);
                        vLine.setVisibility(View.VISIBLE);
                    } else {
                        tvItem.setBackgroundResource(R.color.common_colorWhite);
                        vLine.setVisibility(View.INVISIBLE);
                    }
                }
            };
            mainAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    selectPosition = position;
                    mainAdapter.notifyDataSetChanged();
                    CatChoose catChoose = mainList.get(position);
                    List<CatChoose> childs = catChoose.childs;
//                    if (childs == null || childs.isEmpty()) {
//                        showLog(catChoose);
//                    }
                    initSubAdapter(childs);
                }
            });
            mainRecycler.setAdapter(mainAdapter);
        }
        mainAdapter.notifyDataSetChanged();
    }

    private void initSubAdapter(List<CatChoose> childs) {
        subList.clear();
        if (childs != null) {
            subList.addAll(childs);
        }
        if (subAdapter == null) {
            subAdapter = new BaseAdapter<CatChoose, BaseHolder>(R.layout.item_layout_cat_choose, subList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    CatChoose catChoose = subList.get(position);
                    TextView tvItem = helper.getView(R.id.tvItem);
                    View vLine = helper.getView(R.id.vLine);
                    tvItem.setTextColor(getResources().getColor(R.color.common_colorTextLight));
                    tvItem.setText(catChoose.name);
                    tvItem.setBackgroundResource(R.color.common_colorWhite);
                    vLine.setVisibility(View.INVISIBLE);
                }
            };
            subAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    CatChoose catChoose = mainList.get(selectPosition);
                    CatChoose subChoose = catChoose.childs.get(position);
                    showLog(catChoose);
                    showLog(subChoose);
                    Intent intent = new Intent();
                    intent.putExtra("mainCat", catChoose);
                    intent.putExtra("subCat", subChoose);
                    setResult(RESULT_OK, intent);
                    destroyActivity();
                }
            });
            subRecycler.setAdapter(subAdapter);
        }
        subAdapter.notifyDataSetChanged();
    }
}
